package com.enjoyor.soa.traffic.rest.efoms.dubbo.invoke;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.enjoyor.soa.traffic.api.service.dubbo.collectms.IDubboRealMonitoringService;
import com.enjoyor.soa.traffic.rest.efoms.helper.CheckPojoHelper;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

@Component
public class DubboCollectRealServiceInvoke {

	//@Reference(version = "1.0.0", timeout = 10000) 
	@Reference(version = "1.0.0", timeout = 10000, url="dubbo://172.100.102.37:28803") 
    private IDubboRealMonitoringService dubboRealMonitoringService;
	
	/**
	 * 根据设备ID获取采集设备各车道实时流量数据
	 * @return List<DevWayDataDto>
	 */
	public ResultPojo getDevWay5MinsFlow(String devId) { 
		ResultPojo resultPojo = new ResultPojo();
    	if (null != dubboRealMonitoringService) {
    		ResultPojo pojo = dubboRealMonitoringService.getDevWay5MinsFlow(devId);
			if(CheckPojoHelper.isValid(pojo)) {
				Map<String, Object> resultMap = new HashMap<String, Object>();
				int allFlow = 0;
				int realFlow = 0;
				List<Map<String, Object>> devWayDataMapList = (List<Map<String, Object>>)pojo.getResultList();
				List<String> devWayList = new ArrayList<String>();
				for(Map<String, Object> devWayDataMap : devWayDataMapList) {
					String devWayId = devWayDataMap.get("dev_way_id").toString();
					if(null == devWayId || "".equals(devWayId)) {
						continue;
					}
					String flow = devWayDataMap.get("all_flow").toString();
					allFlow += Integer.parseInt(flow);
					if(devWayList.size() <= 0) {
						devWayList.add(devWayId);
					}else {
						boolean flag = true;
						for(int i=0; i <devWayList.size(); i++ ) {
							if(devWayList.get(i).equals(devWayId)) {
								flag = false;
								break;
							}
						}
						if(flag) {
							devWayList.add(devWayId);
						}
					}
				}
				if(devWayList.size() > 0) {
					for(int i=0; i <devWayList.size(); i++ ) {
						List<Map<String, Object>> tempWayMapList = new ArrayList<Map<String, Object>>();
						for(Map<String, Object> devWayDataMap : devWayDataMapList) {
							String devWayId = devWayDataMap.get("dev_way_id").toString();
							if(devWayList.get(i).equals(devWayId)) {
								String flow = devWayDataMap.get("all_flow").toString();
								String speed = devWayDataMap.get("ave_speed").toString();
								Date recordTimeDD = (Date) devWayDataMap.get("record_time");
								String recordTime = TimeHelper.dateToString(recordTimeDD, "yyyy-MM-dd HH:mm:ss");
								Map<String, Object> mapTemp = new HashMap<String, Object>();
								mapTemp.put("devWayId", devWayId);
								mapTemp.put("recordTime", recordTime);
								mapTemp.put("speed", speed);
								mapTemp.put("flow", flow);
								tempWayMapList.add(mapTemp);
							}
						}
						//Map<String, Object> tempRealMap = tempWayMapList.get(tempWayMapList.size()-1);
						String tempRealFlow = tempWayMapList.get(tempWayMapList.size()-1).get("flow").toString();
						realFlow += Integer.parseInt(tempRealFlow);
						resultMap.put("devWay_" + devWayList.get(i), tempWayMapList);
					}
				}
				resultMap.put("allFlow", allFlow);
				resultMap.put("realFlow", realFlow);
				resultPojo.setResultList(resultMap);
			}
    		
    	}
        return resultPojo;
	}
}
